/*
 * SearchInterface.java
 *
 * Created on 26 novembre 2012, 22:15
 */
 
package com.prototype.vaadin;           

import com.vaadin.Application;
import com.vaadin.terminal.Sizeable;
import com.vaadin.ui.*;
import java.io.File;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
/** 
 *
 * @author Cmd
 * @version 
 */

public class SearchInterface{
    String app_path;
    static private HashMap<String,String> liste = new HashMap<String,String>();
    private ComboBox combo_dt_year_start;
    private ComboBox combo_dt_year_end;
    private ComboBox combo_dt_update_start;
    private ComboBox combo_dt_update_end;
    private Form form;
    private TwinColumnSelect twinCol_categories;
    
    public String getApp_path() {
        return app_path;
    }

    public void setApp_path(String app_path) {
        this.app_path = app_path;
    }
    
    public void RecoverForm(VerticalLayout vlayout_searchinterface) throws Exception{
        // Recovering properties file
        AppProperties prop = new AppProperties(app_path+ File.separator +"recover_sf_code"+ File.separator + "web" + File.separator + "WEB-INF"+ File.separator +"french_label.properties");
        
        // create and initialize the differents objects of the page
        Label lbl_description = new Label("<center>"+" "+ prop.getPropValue("app_description") +"<center>");
        lbl_description.setContentMode(Label.CONTENT_XHTML);
        
        // create the main form
        Form form_recover_sf = new Form();
        form_recover_sf.setDescription("");
        form_recover_sf.setWidth("400");
        
        int combo_size = 60;
        final ComboBox combo_dt_year_start = new ComboBox(prop.getPropValue("combo_dt_year_start"));
        ComboBox combo_dt_year_end = new ComboBox(prop.getPropValue("combo_dt_year_end"));
        ComboBox combo_dt_update_start = new ComboBox(prop.getPropValue("combo_dt_update_start"));
        ComboBox combo_dt_update_end = new ComboBox(prop.getPropValue("combo_dt_update_end"));
        
        combo_dt_year_start.setWidth(combo_size, Sizeable.UNITS_PIXELS);
        combo_dt_year_end.setWidth(combo_size, Sizeable.UNITS_PIXELS);
        combo_dt_update_start.setWidth(combo_size, Sizeable.UNITS_PIXELS);
        combo_dt_update_end.setWidth(combo_size, Sizeable.UNITS_PIXELS);
        
        Calendar calendrier;
	calendrier = Calendar.getInstance();
        
        for(int i=2000; i<=calendrier.get(Calendar.YEAR);i++){
            combo_dt_year_start.addItem(i);
            combo_dt_year_end.addItem(i);
            combo_dt_update_start.addItem(i);
            combo_dt_update_end.addItem(i);
        }
        
        HorizontalLayout horizonLayt_year = new HorizontalLayout();
        horizonLayt_year.addComponent(combo_dt_year_start);
        horizonLayt_year.addComponent(combo_dt_year_end);
        horizonLayt_year.addComponent(combo_dt_update_start);
        horizonLayt_year.addComponent(combo_dt_update_end);
        horizonLayt_year.setMargin(true);
        horizonLayt_year.setSpacing(true);
        
        // add a field and bind it to an named item property
        form_recover_sf.addField("nb_developer", new TextField(prop.getPropValue("nb_developer")));
        form_recover_sf.addField("top_download", new TextField(prop.getPropValue("top_download")));
        form_recover_sf.addField("top_line_code", new TextField(prop.getPropValue("top_line_code")));
        form_recover_sf.addField("nb_lines_code", new TextField(prop.getPropValue("nb_lines_code")));
        form_recover_sf.addField("project_size", new TextField(prop.getPropValue("project_size")));
        
        VerticalLayout verticalLayt = new VerticalLayout();
        verticalLayt.addComponent(horizonLayt_year);
        verticalLayt.addComponent(form_recover_sf);
        verticalLayt.setMargin(true);
        verticalLayt.setSpacing(true);
        
        CheckBox chbx_developer = new CheckBox(prop.getPropValue("chbx_developer"));

        // Recover SF data
        //Categories
        AppProperties prop_sf = new AppProperties(app_path+ File.separator +"recover_sf_code"+ File.separator + "web" + File.separator + "WEB-INF"+ File.separator +"sf_data.properties");
        Document sf_page = Jsoup.connect(prop_sf.getPropValue("sf_index")).timeout(0).get();
        Elements menuitems = sf_page.select("#metacats li a");
        final TwinColumnSelect twinCol_categories = new TwinColumnSelect(menuitems);
        twinCol_categories.setCaption(prop.getPropValue("twinCol_categories"));
        
        //Licenses
        sf_page = Jsoup.connect(prop_sf.getPropValue("sf_criteria_page")).timeout(0).get();
        menuitems = sf_page.select("div .facet header");
        Iterator<Element> iterator = menuitems.iterator();
        TwinColumnSelect twinCol_licenses = new TwinColumnSelect();
        
        while(iterator.hasNext()){
            Element item = iterator.next();
            if (item.html().substring(0, 7).equals("License")) {
                twinCol_licenses = new TwinColumnSelect(item.parent().select("ul li ul li a"));
                twinCol_licenses.setCaption(prop.getPropValue("twinCol_license"));
            }
        }
        
        //OS
        iterator = menuitems.iterator();
        TwinColumnSelect twinCol_os = new TwinColumnSelect();
        while(iterator.hasNext()){
            Element item = iterator.next();
            if (item.html().substring(0, 2).equals("OS")) {
                Elements oslist = new Elements();
                for(int i=0; i<item.parent().select("ul li span").size(); i++){
                    System.out.println(item.parent().select("ul li ").get(i)+" "+item.parent().select("ul li span").get(i).attr("class") == "ui-icon ui-icon-triangle-1-e");
                    if(item.parent().select("ul li span").get(i).attr("class") == "ui-icon ui-icon-triangle-1-e"){
                        System.out.println("DEDANS____________________");
                        oslist.add(item.parent().select("ul li ul li a").get(i));
                    }else{
                        oslist.add(item.parent().select("ul li a").get(i));
                    }
                }
                twinCol_os = new TwinColumnSelect(oslist);
                twinCol_os.setCaption(prop.getPropValue("twinCol_os"));
            }
        }
        
        Button commit_search = new Button(prop.getPropValue("commit_search"));
        commit_search.addListener(Button.ClickEvent.class, this, "affiche");
        
        //placement of objects in the page
        GridLayout gridLayoutMain = new GridLayout(2, 6);
        gridLayoutMain.setStyleName("gridLayoutMain");
        gridLayoutMain.setWidth(100, Sizeable.UNITS_PERCENTAGE);
//        gridLayoutMain.setColumnExpandRatio(0, (float)Math.pow(2,-1000000000));
//        gridLayoutMain.setColumnExpandRatio(1, 1);
//        gridLayoutMain.setSizeFull();
        
        gridLayoutMain.addComponent(twinCol_categories, 0, 0);
        gridLayoutMain.addComponent(twinCol_licenses, 0, 1);
        gridLayoutMain.addComponent(twinCol_os, 0, 2);
        gridLayoutMain.addComponent(verticalLayt, 1, 0, 1, 2);
//        gridLayoutMain.addComponent(form_recover_sf, 1, 1);
        gridLayoutMain.addComponent(commit_search, 0, 3, 1, 3);
       // gridLayoutMain.addComponent(horizonLayt_update, 1, 1);
//        gridLayoutMain.addComponent(chbx_developer, 1, 2);                
        
        // objects positionnement
        gridLayoutMain.setComponentAlignment(commit_search, Alignment.MIDDLE_CENTER);
        gridLayoutMain.setComponentAlignment(verticalLayt, Alignment.TOP_CENTER);
       // gridLayoutMain.setComponentAlignment(horizonLayt_update, Alignment.MIDDLE_CENTER);
//        gridLayoutMain.setComponentAlignment(form_recover_sf, Alignment.TOP_CENTER);
        gridLayoutMain.setMargin(true);
        gridLayoutMain.setSpacing(true);
        
        vlayout_searchinterface.addComponent(lbl_description);
        vlayout_searchinterface.addComponent(gridLayoutMain);
    }
    
    public HashMap formRecover(){
        HashMap<String,String> li = new HashMap<String, String>();
        System.out.println("combo_dt_year_start get:"+combo_dt_year_start.toString() );
        System.out.println("textfield nbdevloppeur vide :"+form.getField("nb_developer").toString().length() +"========twin:"+twinCol_categories.getSelected());

        if(combo_dt_year_start.toString()!=null) {
            li.put("combo_dt_year_start", combo_dt_year_start.getValue().toString());
        }
        if(combo_dt_year_end.toString()!=null) {
            li.put("combo_dt_year_end", combo_dt_year_end.getValue().toString());
        }
        if(combo_dt_update_start.toString()!=null) {
            li.put("combo_dt_update_start", combo_dt_update_start.getValue().toString());
        }
        if(combo_dt_update_end.toString()!=null) {
            li.put("combo_dt_update_end", combo_dt_update_end.getValue().toString());
        }     
        
        String nb_developer;
        if( ((nb_developer=form.getField("nb_developer").toString()).length() )!=0) {
            li.put("nb_developer", nb_developer);
        }
        String top_download;
        if( ((top_download=form.getField("top_download").toString()).length() )!=0) {
            li.put("top_download", top_download);
        }
        String top_line_code;
        if( (top_line_code=form.getField("top_line_code").toString()).length()!=0) {
            li.put("top_line_code", top_line_code);
        }
        String nb_lines_code;
        if( ((nb_lines_code=form.getField("nb_lines_code").toString()).length() )!=0) {
            li.put("nb_lines_code", nb_lines_code);
        }
        String project_size;
        if( ((project_size=form.getField("project_size").toString()).length() )!=0) {
            li.put("project_size", project_size);
        }
        
        return li;
    }
    public void affiche(Button.ClickEvent event){
        
        /*
         * getMainWindow().showNotification(
                           
                                 "combo_dt_year_start"+combo_dt_year_start.getValue().toString()+"<br>"+
                                 "combo_dt_year_end"+combo_dt_year_end.getValue().toString()+"<br>"+
                                 "combo_dt_update_start"+combo_dt_update_start.getValue().toString()+"<br>"+ 
                                 "combo_dt_update_end"+combo_dt_update_end.getValue().toString(),
                                com.vaadin.ui.Window.Notification.TYPE_WARNING_MESSAGE);
    }
    * /*/
        System.out.println("ma liste de zmar est"+ formRecover());
        
    }
}
